package com.itc.bi.api.dto;

import com.itc.bi.api.constant.SystemConstant;
import com.itc.common.core.exception.ServiceException;
import lombok.Data;
import java.util.Collection;
import java.util.List;

@Data
public class ParamsDto {

    //字段名称
    String columnName;
    //操作类型 等于:eq 不等于:ne 属于:in 不属于:noin 包含:like 不包含:nolike 空:null 非空:nonull 大于:gt 大于等于:ge 小于:lt 小于等于:le
    String type;
    //值,当类型是属于或不属于时,该值为列表
    Object value;

    public String getExp(){
        switch (type){
            case SystemConstant.FILTER_CONDITION_EQ:
                return columnName+" = '"+value+"'";
            case SystemConstant.FILTER_CONDITION_NE:
                return columnName+" != '"+value+"'";
            case SystemConstant.FILTER_CONDITION_IN:
                if(value instanceof Collection){
                    List values =(List)value;
                    StringBuffer inBuffer = new StringBuffer(columnName).append(" in (");
                    for (int i = 0; i < values.size(); i++) {
                        if(i > 0){
                            inBuffer.append(",");
                        }
                        inBuffer.append("'").append(values.get(i)).append("'");
                    }
                    inBuffer.append(" )");
                    return inBuffer.toString();
                }else{
                    return null;
                }
            case SystemConstant.FILTER_CONDITION_NOIN:
                if(value instanceof Collection){
                    List notValues =(List)value;
                    StringBuffer notInBuffer = new StringBuffer(columnName).append(" not in (");
                    for (int i = 0; i < notValues.size(); i++) {
                        if(i > 0){
                            notInBuffer.append(",");
                        }
                        notInBuffer.append("'").append(notValues.get(i)).append("'");
                    }
                    notInBuffer.append(" )");
                    return notInBuffer.toString();
                }else{
                    return null;
                }
            case SystemConstant.FILTER_CONDITION_LIKE:
                return columnName+" like '%"+value+"%'";
            case SystemConstant.FILTER_CONDITION_NOLIKE:
                return columnName+" not like '%"+value+"%'";
            case SystemConstant.FILTER_CONDITION_NULL:
                return columnName+" is null";
            case SystemConstant.FILTER_CONDITION_NONULL:
                return columnName+" is not null";
            case SystemConstant.FILTER_CONDITION_GT:
                return columnName+" > '"+value+"'";
            case SystemConstant.FILTER_CONDITION_GE:
                return columnName+" >= '"+value+"'";
            case SystemConstant.FILTER_CONDITION_LT:
                return columnName+" < '"+value+"'";
            case SystemConstant.FILTER_CONDITION_LE:
                return columnName+" <= '"+value+"'";
            default:throw new ServiceException(String.format("暂不支持 %s 的表达式",type));
        }
    }
}
